from app import create_app
from app.control.extends_app import db
from flask_script import Manager
from flask_migrate import MigrateCommand


app = create_app("development")
manager = Manager(app)
manager.add_command("db", MigrateCommand)


@manager.command
def createdb():
    db.create_all()
    return "数据库创建成功"


@manager.command
def dropdb():
    db.drop_all()
    return "数据库删除成功"


# 初始化管理员账号
@manager.command
def create_admin():
    from app.model.models import ManagerUser
    tuser = ManagerUser.query.filter_by(username="admin").first()
    if not tuser:
        db.session.add(ManagerUser(username="admin", password="123456"))
        return "Manager create success!"
    else:
        return "The created manager is exist!"


if __name__ == "__main__":
    manager.run()
